import 'dart:ui';

import '../utils/color_utils.dart';
import 'base/base_effect.dart';
import 'effect_template.dart';

class Effect05 extends EditableAnimationEffect {
  @override
  EffectType get effectTyp => EffectType.effect05;

  @override
  List<Color> get defaultColors => const [Color(0xFFFF0000), Color(0xFFFFFFFF)];

  @override
  bool get enableAngle => true;

  @override
  RangeArgument get angleRange => RangeArgument(-180, 180, 0);

  @override
  bool get enableColorWidth => false;

  @override
  bool get enableDensity => false;

  @override
  bool get enableEditColor => true;

  @override
  bool get enableSpeed => true;

  @override
  RangeArgument get speedRange => RangeArgument(0, 9, 9);

  @override
  Future<List<List<List<Color>>>> getFrames({EffectOperation? operation}) {
    var setColorList = getColorList(effectOperation: operation);
    var colorList = [
      const Color(0xFF000000),
      setColorList[0],
      setColorList[1],
      setColorList[0],
      const Color(0xFF000000),
    ];

    int angle = getAngle(effectOperation: operation);

    Size rotateSize = getRotateSize(angle: angle);
    double newWidth = rotateSize.width;
    double newHeight = rotateSize.height;

    ///彩色行数
    double colorfulCount = newHeight;

    ///底部黑色行数
    double blackCount = newHeight * 0.4;

    int totalFrame = (colorfulCount + blackCount).toInt();

    List<double> stops = [
      0.0,//black
      colorfulCount*0.2,//red
      colorfulCount*0.5,//white
      colorfulCount*0.8,//red
      colorfulCount,//black
    ];

    ///先计算单列的颜色排布
    List<Color> singleColumn = [];
    for (int i = 0; i < totalFrame; i++) {
      if(i<=colorfulCount){
        var gradientColor = ColorUtils.lerpGradient(colorList, stops, i.toDouble());
        singleColumn.add(gradientColor);
      }else{
        singleColumn.add(const Color(0xFF000000));
      }
    }

    List<List<List<Color>>> frames = [];

    for (int i = 0; i < totalFrame; i++) {
      List<List<Color>> frame = [];
      int count = (newWidth * 0.5).toInt();
      for (int j = 0; j < count; j++) {
        List<Color> columns = [];
        for (int z = 0; z < newHeight; z++) {
          columns.add(singleColumn[(i + z - j) % singleColumn.length]);
        }
        frame.insert(0, columns);
        frame.add(columns);
      }
      frames.add(frame);
    }

    if (angle != 0) {
      List<List<List<Color>>> angleFrames = getRotateFrames(
        frames,
        angle: angle,
        newWidth: newWidth,
        newHeight: newHeight,
      );
      return Future.value(angleFrames);
    } else {
      return Future.value(frames);
    }
  }

  @override
  bool get isFixedColorCount => true;

  @override
  Size get size => const Size(24, 24);
}